package utils.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.FocusEntity;

import tools.ItemOnClickListenerOther;
import utils.roundimage.RoundNetWorkImageView;
import volley.VolleySingleton;


/**
 * 关注适配器
 * Created by dllo on 15/11/22.
 */
public class FocusonAdpter extends RecyclerView.Adapter<FocusonAdpter.RecylerAdpter> {
    private List<FocusEntity.DataEntity> datas;
    private ItemOnClickListenerOther listener;
    private Context context;

    public FocusonAdpter(Context context) {
        this.context = context;
    }

    public void setItemOnClick(ItemOnClickListenerOther listener) {
        this.listener = listener;
    }

    public void addDatas(List<FocusEntity.DataEntity> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }


    //加载布局
    @Override
    public RecylerAdpter onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.focuson_item, null);
        return new RecylerAdpter(view);
    }

    @Override
    public void onBindViewHolder(FocusonAdpter.RecylerAdpter holder, int position) {

        if (datas != null && datas.size() > 0) {
            FocusEntity.DataEntity frData = datas.get(position);
            holder.itemPosition = position;
            //获得值
            holder.focusonItemTopTitle.setText(frData.getAlbumTitle());
            holder.focusonItemTopContent.setText(frData.getTrackTitle());
            VolleySingleton.getInstance().getBitmap(frData.getAlbumCover(), holder.focusonItemImg);
            VolleySingleton.getInstance().getBitmap(frData.getAvatar(), holder.focusonItemUserImg);
            holder.focusonItemBottonTitle.setText(frData.getNickname());
            holder.focusonItemBottonContent.setText(frData.getPersonalSignature());
            holder.focusonItemUserGz.setText("  + 关注  ");
        }
    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    public class RecylerAdpter extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView focusonItemTopTitle, focusonItemUserGz, focusonItemTopContent, focusonItemBottonTitle, focusonItemBottonContent;
        private NetworkImageView focusonItemImg;
        private RoundNetWorkImageView focusonItemUserImg;
        private LinearLayout focusonItemBotton, focusonItemTop;
        private int itemPosition;

        public RecylerAdpter(View itemView) {
            super(itemView);
            focusonItemImg = (NetworkImageView) itemView.findViewById(R.id.focusonItemImg);
            focusonItemTopTitle = (TextView) itemView.findViewById(R.id.focusonItemTopTitle);
            focusonItemTopContent = (TextView) itemView.findViewById(R.id.focusonItemTopContent);
            focusonItemBottonTitle = (TextView) itemView.findViewById(R.id.focusonItemBottonTitle);
            focusonItemBottonContent = (TextView) itemView.findViewById(R.id.focusonItemBottonContent);
            focusonItemUserImg = (RoundNetWorkImageView) itemView.findViewById(R.id.focusonItemUserImg);
            focusonItemUserGz = (TextView) itemView.findViewById(R.id.focusonItemUserGz);
            focusonItemBotton = (LinearLayout) itemView.findViewById(R.id.focusonItemBotton);
            focusonItemTop = (LinearLayout) itemView.findViewById(R.id.focusonItemTop);
            focusonItemUserGz.setOnClickListener(this);
            focusonItemTop.setOnClickListener(this);
            focusonItemBotton.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (datas != null) {
                FocusEntity.DataEntity f = datas.get(itemPosition);
                if (listener != null) {
                    switch (v.getId()) {
                        case R.id.focusonItemUserGz:
                            listener.itemListener(f, 1);
                            break;
                        case R.id.focusonItemTop:
                            listener.itemListener(f, 2);
                            break;
                        case R.id.focusonItemBotton:
                            listener.itemListener(f, 3);
                            break;
                    }
                }
            }
        }
    }

}
